<template>
  <div class="projects-container">
    <!-- 顶部操作栏 -->
    <div class="top-bar">
      <h2 class="title">我的项目</h2>
      <el-button
        v-if="isAdmin()"
        type="primary"
        icon="el-icon-plus"
        @click="createNewProject"
      >
        新建项目
      </el-button>
    </div>

    <!-- 项目表格 -->
    <el-table
      :data="ownProjects"
      style="width: 100%"
      :header-cell-style="{ background: '#f5f7fa' }"
      v-loading="loading"
    >
      <el-table-column prop="projectId" label="ID" width="80" />
      <el-table-column prop="name" label="项目名称" min-width="200">
        <template #default="scope">
          <el-link type="primary" @click="gotoProject(scope.row.projectId)">
            {{ scope.row.name }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" min-width="300" show-overflow-tooltip />
<!--      <el-table-column prop="createTime" label="创建时间" width="180" />-->
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'PUBLIC' ? 'success' : 'info'">
            {{ scope.row.status === 'PUBLIC' ? '公开' : '私密' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button
            size="mini"
            type="primary"
            @click="gotoProject(scope.row.projectId)"
          >
            进入项目
          </el-button>
          <el-button
            v-if="isAdmin()"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <div class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="ownListCount"
        :current-page.sync="currentPage"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script>
import { mapActions, mapGetters, mapMutations } from 'vuex'

export default {
  name: 'HomeProjects',
  data() {
    return {
      loading: false,
      currentPage: 1
    }
  },
  computed: {
    ...mapGetters(['ownProjects', 'ownListCount', 'ownPageNo', 'userInfo'])
  },
  methods: {
    ...mapMutations(['setOwnPageNo', 'setShowCreatePanel']),
    ...mapActions(['getOwnListByPageNo', 'deleteProject']),

    createNewProject() {
      this.setShowCreatePanel(true)
      this.$root.$once('project-created', () => {
        this.loading = true
        this.getOwnListByPageNo({
          userId: this.userInfo.id,
          pageNo: this.currentPage
        }).finally(() => {
          this.loading = false
          this.$message.success('项目创建成功')
        })
      })
    },

    gotoProject(id) {
      this.$router.push(`/graph/${id}`)
    },

    handlePageChange(page) {
      this.loading = true
      this.setOwnPageNo(page)
      this.getOwnListByPageNo({
        userId: this.userInfo.id,
        pageNo: page
      }).finally(() => {
        this.loading = false
      })
    },

    handleDelete(row) {
      this.$confirm('确认删除该项目?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里添加删除项目的逻辑
        console.log('删除项目:', row.projectId)
        this.deleteProject(row.projectId)
          .then(() => {
            this.refreshList()
            this.$message.success('项目删除成功')
          })
          .catch((error) => {
            this.$message.error('项目删除失败: ' + error.message)
          })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 判断角色是否是管理员
    isAdmin() {
      console.log("角色信息："+this.$store.getters.role || localStorage.getItem('user-role'));
      return this.$store.getters.role === 'ADMIN' || localStorage.getItem('user-role') === 'ADMIN';
    },

    refreshList() {
      this.loading = true
      this.getOwnListByPageNo({
        userId: this.userInfo.id,
        pageNo: this.currentPage
      }).finally(() => {
        this.loading = false
        this.$message.success('项目列表刷新成功')
      })
    }
  },
  created() {
    this.$root.$on('project-created', this.refreshList)
  },
  beforeDestroy() {
    this.$root.$off('project-created', this.refreshList)
  }
}
</script>

<style scoped>
.projects-container {
  padding: 20px 100px 100px 100px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.title {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
  color: #303133;
}

.pagination-container {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-button--mini) {
  padding: 7px 12px;
}

:deep(.el-table__header) {
  background-color: #f5f7fa;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled).active) {
  background-color: #409EFF;
  color: #fff;
}
</style>
